//
//  HUD.m
//  beautyAccompany
//
//  Created by xiaocankeji on 15/11/3.
//  Copyright © 2015年 xiaocankeji. All rights reserved.
//

#import "HUD.h"

@implementation HUD

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
-(UIView*)HUDImage :(UIImage*)image Title:(NSString*)title Content:(NSString*)content CANCEL:(NSString*)Cancel Confirm:(NSString*)confirm{
    
    toolbar=[[UIToolbar alloc]initWithFrame:[UIApplication sharedApplication].delegate.window.bounds];
    [toolbar setBarStyle:UIBarStyleBlack];
    toolbar.alpha=0;
    [[UIApplication sharedApplication].delegate.window addSubview:toolbar];
    
    [UIView animateWithDuration:0.2
                     animations:^{
                         toolbar.alpha=1;
                     }];
    
    View_screen=[[UIView alloc]init];
    View_screen.backgroundColor=[UIColor whiteColor];
    [View_screen.layer setCornerRadius:5];
    //View_screen.clipsToBounds = YES;
    [toolbar addSubview:View_screen];
    
    if (image==nil) {
        View_screen.frame=CGRectMake(20, (DeviceHeight-200)/2, DeviceWidth-40, 150);
    }else{
        View_screen.frame=CGRectMake(20, (DeviceHeight-200)/2, DeviceWidth-40, 160);
    }
    
    if (image) {
        UIImageView*Imageview_screen=[[UIImageView alloc]initWithFrame:CGRectMake(((DeviceWidth-40)-320)/2, -120, 320, 150)];
        Imageview_screen.image=image;
        [Imageview_screen setContentMode:UIViewContentModeScaleAspectFit];
        [View_screen addSubview:Imageview_screen];
    }
    
    
    
    UILabel*Label_screen=[[UILabel alloc]init];
    Label_screen.font=[UIFont fontWithName:FontName size:20];
    Label_screen.textColor=[UIColor blackColor];
    Label_screen.text=title;
    Label_screen.adjustsFontSizeToFitWidth = YES;
    Label_screen.textAlignment=NSTextAlignmentCenter;
    Label_screen.numberOfLines=2;
    [View_screen addSubview:Label_screen];
    Label_screen.frame=CGRectMake(0, View_screen.bounds.size.height-135, DeviceWidth-40, 30);
    
    if (content==nil){
        Label_screen.frame=CGRectMake(0, View_screen.bounds.size.height-135, DeviceWidth-40, 30);
    }
    
    UILabel*Label_screen2=[[UILabel alloc]initWithFrame:CGRectMake(10, View_screen.bounds.size.height-105, DeviceWidth-60, 55)];
    Label_screen2.font=[UIFont fontWithName:FontName size:15];
    Label_screen2.textColor=[UIColor grayColor];
    Label_screen2.text=content;
    Label_screen2.numberOfLines = 2;
    Label_screen2.textAlignment=NSTextAlignmentCenter;
    [View_screen addSubview:Label_screen2];
    
    
    BFPaperButton*btn_Confirm=[[BFPaperButton alloc]initWithFrame:CGRectMake((DeviceWidth-40)/2, View_screen.bounds.size.height-40, (DeviceWidth-40)/2, 40)raised:NO];
    [btn_Confirm addTarget:self action:@selector(btn_Confirm) forControlEvents:UIControlEventTouchUpInside];
    [btn_Confirm setTitle:confirm forState:UIControlStateNormal];
    [btn_Confirm setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
    [btn_Confirm setBackgroundColor:[UIColor whiteColor]];
    //    btn_Confirm.usesSmartColor = YES;
    [View_screen addSubview:btn_Confirm];
    
    if (Cancel!=nil) {
        BFPaperButton*btn_Cancel=[[BFPaperButton alloc]initWithFrame:CGRectMake(0, View_screen.bounds.size.height-40, (DeviceWidth-40)/2, 40)raised:NO];
        [btn_Cancel setTitle:Cancel forState:UIControlStateNormal];
        [btn_Cancel addTarget:self action:@selector(btn_Cancel) forControlEvents:UIControlEventTouchUpInside];
        [btn_Cancel setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:1]];
        [btn_Cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //        btn_Cancel.usesSmartColor = NO;
        [View_screen addSubview:btn_Cancel];
    }else{
        btn_Confirm.frame=CGRectMake(0, View_screen.bounds.size.height-40, View_screen.bounds.size.width, 40);
    }
    // 分隔线
    for (int i = 0; i < 2; i++) {
        UIView *Line = [[UIView alloc]initWithFrame:CGRectMake(0, View_screen.bounds.size.height-40, View_screen.bounds.size.width, 0.5)];
        Line.backgroundColor = RGBA(229, 229, 229, 1);
        if (i>=1) {
            Line.frame = CGRectMake((DeviceWidth-40)/2, View_screen.bounds.size.height-40, 0.5, 40);
        }
        [View_screen addSubview:Line];
    }
    return toolbar;
    
}
-(void)btn_Cancel
{
    [self hide];
    if (self.Cancel) {
        self.Cancel();
    }
    
}

-(void)btn_Confirm
{
    [self hide];
    if (self.Confirm) {
        self.Confirm();
    }
}
-(void)hide
{
    [Utils animationSpringBounciness:20 View:View_screen Rect:CGRectMake(20, DeviceHeight+200, DeviceWidth-40, 200)];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.2
                         animations:^{
                             toolbar.alpha = 0;
                             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                 [toolbar removeFromSuperview];
                             });
                         }];
    });
    
}


+ (MBProgressHUD *)setOnViewWithTitle:(NSString *)title animated:(BOOL)animated
{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].delegate.window animated:YES];
    if (title&&title.length) {
        hud.labelText = title;
    }
    hud.dimBackground = YES;
    [hud hide:YES];
    return hud;
}

//------------------------------------
// Hide the leader in view
//------------------------------------
+ (BOOL)hideFromViewAnimated:(BOOL)animated {
    MBProgressHUD *hud = [MBProgressHUD HUDForView:[UIApplication sharedApplication].delegate.window];
    if (hud) {
        //        [[UIApplication sharedApplication].delegate.window setUserInteractionEnabled:YES];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [hud removeFromSuperview];
        return YES;
    }
    return NO;
}
+ (HUD *)HUDForView: (UIView *)view {
    HUD *hud = nil;
    NSArray *subViewsArray = view.subviews;
    Class hudClass = [HUD class];
    for (UIView *aView in subViewsArray) {
        if ([aView isKindOfClass:hudClass]) {
            hud = (HUD *)aView;
        }
    }
    return hud;
}
- (void)drawBackgroundCircle:(BOOL) partial {
    CGFloat startAngle = - ((float)M_PI / 2); // 90 Degrees
    CGFloat endAngle = (32 * (float)M_PI) + startAngle;
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    CGFloat radius = (self.bounds.size.width - _lineWidth)/2;
    
    //----------------------
    // Begin draw background
    //----------------------
    
    UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
    processBackgroundPath.lineWidth = _lineWidth;
    
    //---------------------------------------
    // Make end angle to 90% of the progress
    //---------------------------------------
    if (partial) {
        endAngle = (1.8f * (float)M_PI) + startAngle;
    }
    [processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    _backgroundLayer.path = processBackgroundPath.CGPath;
}

#pragma mark - Spin
- (void)start {
    //    self.isSpinning = YES;
    [self drawBackgroundCircle:YES];
    
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = HUGE_VALF;
    [_backgroundLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

- (void)stop{
    [self drawBackgroundCircle:NO];
    [_backgroundLayer removeAllAnimations];
    //    self.isSpinning = NO;
}

//提示框
+(void)Prompt:(NSString*)prompt
{
    CGSize size = [prompt sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:FontName size:14],NSFontAttributeName, nil]];
    CGFloat f = size.width;
    
    if (f>DeviceWidth - 50) {
        f = DeviceWidth - 50;
    }
    
    UILabel*Label_Prompt=[[UILabel alloc]initWithFrame:CGRectMake((DeviceWidth-(f+30))/2, (DeviceHeight-50)/2, f+30, 50)];
    Label_Prompt.text=prompt;
    Label_Prompt.numberOfLines = 0;
    Label_Prompt.font=[UIFont fontWithName:FontName size:14];
    Label_Prompt.textAlignment=NSTextAlignmentCenter;
    Label_Prompt.textColor=[UIColor whiteColor];
    Label_Prompt.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.7];
    Label_Prompt.layer.masksToBounds = YES;//开启圆角
    [Label_Prompt.layer setCornerRadius:7];
    [[UIApplication sharedApplication].delegate.window  addSubview:Label_Prompt];
    
    [Utils Hidden:Label_Prompt Bool:YES];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [Utils Hidden:Label_Prompt Bool:NO];
    });
    
    
}

@end
